package com.snj.visangedu.studytime;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.snj.visangedu.R;

public class Timepanel extends RelativeLayout implements OnClickListener {
	
	ImageView img_up = null;
	ImageView img_10 = null;
	ImageView img_1 = null;
	ImageView img_down = null;
	
	private int timetype = 0;	// 0 : HOUR, 1 : MIN, 2 : SEC
	private int time = 0;
	
	private boolean isrunning = false;
	
	public Timepanel(Context context)
	{
		super(context);
		init();
	}
	
	public Timepanel(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init();
	}
	
	private void init()
	{
		LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li.inflate(R.layout.studytimer_timepanel, this, true);
		
		img_up = (ImageView)findViewById(R.id.img_up);
		img_10 = (ImageView)findViewById(R.id.img_10);
		img_1 = (ImageView)findViewById(R.id.img_1);
		img_down = (ImageView)findViewById(R.id.img_down);
		
		img_up.setOnClickListener(this);
		img_down.setOnClickListener(this);
	}
	
	public void SetTimeType(int timetype)
	{
		this.timetype = timetype;
	}
	
	public int GetTimeType()
	{
		return this.timetype; 
	}
	
	public void SetTime(int _time)
	{
		this.time = _time;
		
		this.time %= 100;
		
		img_10.setImageResource(getContext().getResources().getIdentifier("n" + ((int)(this.time / 10)) + "_" + (isrunning ? "b" : "g"), "drawable", getContext().getPackageName()));
		img_1.setImageResource(getContext().getResources().getIdentifier("n" + ((int)(this.time % 10)) + "_" + (isrunning ? "b" : "g"), "drawable", getContext().getPackageName()));
		
	}
	
	public int GetTime()
	{
		return this.time;
	}
	
	public void SetRunningState(boolean state)
	{
		isrunning = state;
		
		img_up.setVisibility(isrunning ? View.INVISIBLE : View.VISIBLE);
		img_down.setVisibility(isrunning ? View.GONE : View.VISIBLE);
	}
	
	public boolean GetRunningState(boolean state)
	{
		return isrunning;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId())
		{
		case R.id.img_up:
			if(timetype == 2 || timetype == 1) // ���̳� �� �ϰ�� % 60
			{
				time += 5;
				time %= 60;
			}
			else
			{
				time++;
			}
			
			time %= 100;
			
			Log.d("Timepanel", "time(" + timetype + " : " + time);
			
			img_10.setImageResource(getContext().getResources().getIdentifier("n" + ((int)(time / 10)) + "_" + (isrunning ? "b" : "g"), "drawable", getContext().getPackageName()));
			img_1.setImageResource(getContext().getResources().getIdentifier("n" + ((int)(time % 10)) + "_" + (isrunning ? "b" : "g"), "drawable", getContext().getPackageName()));
			
			
			break;
		case R.id.img_down:
			Log.d("Timepanel", "btime(" + timetype + ") : " + time);
			if(timetype == 2 || timetype == 1) // ���̳� �� �ϰ�� % 60
			{
				time -= 5;
				time = (time < 0 ? 60 + time : time);
			}
			else
			{
				time--;
				time = (time < 0 ? 0 : time);
			}
			
			Log.d("Timepanel", "atime(" + timetype + ") : " + time);
			
			img_10.setImageResource(getContext().getResources().getIdentifier("n" + ((int)(time / 10)) + "_" + (isrunning ? "b" : "g"), "drawable", getContext().getPackageName()));
			img_1.setImageResource(getContext().getResources().getIdentifier("n" + ((int)(time % 10)) + "_" + (isrunning ? "b" : "g"), "drawable", getContext().getPackageName()));
			break;
		}
	}

}
